#include <interfacelib/window/ImageMenu.h>

using namespace interfacelib::Window;

ImageMenu::ImageMenu() : m_currentMousePointMenu( NULL )
{
}

ImageMenu::~ImageMenu()
{
	release();
}

bool ImageMenu::createCursor( const char *imageName, BYTE alphaValue )
{
	m_cursorWindow = new interfacelib::Window::Window;

	m_cursorWindow->create( imageName, 0, 0, interfacelib::Window::Window::DISABLEINPUT );
	m_cursorWindow->setAlphaValue( alphaValue );

	add( m_cursorWindow );

	return ( true );
}

void ImageMenu::addMenu( int index, RECT *rect, void *reserve )
{
	{
		MenuItem *item = new MenuItem;
		memcpy( &item->rect, rect, sizeof( RECT ) );
		item->index = index;
		item->reserve = reserve;

		m_menuItemList.push_back( item );
	}
}

void ImageMenu::release()
{
	{
		dequeMenuItem::iterator iter_end( m_menuItemList.end() );
		for ( dequeMenuItem::iterator iter = m_menuItemList.begin() ; iter != iter_end; ++iter )
		{
			MenuItem *item = ( *iter );
			SAFE_DELETE( item );
		}
		m_menuItemList.clear();
	}
}

void ImageMenu::removeMenu( int index )
{
	{
		dequeMenuItem::iterator iter_end( m_menuItemList.end() );
		for ( dequeMenuItem::iterator iter = m_menuItemList.begin() ; iter != iter_end; ++iter )
		{
			MenuItem *item = ( *iter );
			if ( item->index == index )
			{
				m_menuItemList.erase( iter );
				SAFE_DELETE( item );
				return;
			}
		}
	}
}

void ImageMenu::setMenuItem( int index , RECT *rect, void *reserve )
{
	{
		dequeMenuItem::iterator iter_end( m_menuItemList.end() );
		for ( dequeMenuItem::iterator iter = m_menuItemList.begin() ; iter != iter_end; ++iter )
		{
			MenuItem *item = ( *iter );
			if ( item->index == index )
			{
				if ( rect )
				{
					memcpy( &item->rect, rect, sizeof( RECT ) );
				}
				if ( reserve )
				{
					item->reserve = reserve;
				}
				return;
			}
		}
	}
}

void *ImageMenu::getItemData( int index )
{
	{
		dequeMenuItem::iterator iter_end( m_menuItemList.end() );
		for ( dequeMenuItem::iterator iter = m_menuItemList.begin() ; iter != iter_end; ++iter )
		{
			MenuItem *item = ( *iter );
			if ( item->index == index )
			{
				return ( item->reserve );
			}
		}
	}
	return ( NULL );
}


ImageMenu::MenuItem *ImageMenu::getMenuItemPos( DWORD  x, DWORD y )
{
	POINT pt;
	getWindowPos( &pt );

	pt.x = x - pt.x;
	pt.y = y - pt.y;

	MenuItem *item = NULL;
	dequeMenuItem::const_iterator iter_end( m_menuItemList.end() );
	for ( dequeMenuItem::const_iterator iter = m_menuItemList.begin(); iter != iter_end; ++iter )
	{
		item = ( *iter );

		if ( PtInRect( &item->rect, pt ) ) return ( item );
	}
	return ( NULL );
}

void ImageMenu::onMouseMove( UINT flags, POINT point )
{
	Window::onMouseMove( flags, point );

	m_currentMousePointMenu = getMenuItemPos( point.x , point.y );
	if ( m_currentMousePointMenu )
	{
		m_cursorWindow->setVisible( true );
		m_cursorWindow->setWindowPos( m_currentMousePointMenu->rect.left, m_currentMousePointMenu->rect.top );
	}
	else
	{
		m_cursorWindow->setVisible( false );
	}
}

void ImageMenu::onLButtonUp( UINT flags, POINT point )
{
	Window::onLButtonUp( flags, point );

	m_currentMousePointMenu = getMenuItemPos( point.x , point.y );

	if ( m_currentMousePointMenu )
	{
		postMessage( WM_MEMU_SELECT, ( WPARAM )m_currentMousePointMenu->index , ( LPARAM )this );
	}
}

void ImageMenu::onMouseOut( LONG x, LONG y )
{
	Window::onMouseOut( x, y );
	m_currentMousePointMenu = NULL;
}